/*
 * @Author: yuanweijiang
 * @Date: 2025-01-06 14:04:42
 * @LastEditors: yuanweijiang
 */
import { remove } from 'nprogress';
import { getCameraPosition } from './common.js';
import { goToHome } from './scene.js';
let PolygonNameList = [];
let geoPointLayerList = [];
let modelList = [];
let labelLayerList = [];
export const heatmapFn = {
	PolygonName(opt, call) {
		var projectLayer = new SSmap.ProjectionLayer();
		if (opt.name) {
			projectLayer.objectName = opt.name;
		}
		const threeArea2 = new SSmap.ProjectionDataSource();
		if (opt.color) {
			threeArea2.setColor(SSmap.Color.fromRgb(opt.color[0], opt.color[1], opt.color[2], 180));
			threeArea2.setStrokeColor(SSmap.Color.fromRgb(opt.color[0], opt.color[1], opt.color[2], 180)); //描边的透明度 只能是255
		} else {
			threeArea2.setColor(SSmap.Color.fromRgb(255, 255, 255, 127));
			threeArea2.setStrokeColor(SSmap.Color.fromRgb(255, 255, 255, 127));
		}
		projectLayer.limitAltitude = 5000;
		if (opt.addString) {
			threeArea2.addString(opt.addString);
			call(true);
		} else {
			threeArea2.loadGeoJson(opt.url);
		}
		threeArea2.addField('ADDRESS');
		projectLayer.sceneMode = SSmap.TextureProjectionSceneMode.WholeScene; //设置仅建筑
		projectLayer.addDataSource(threeArea2);
		// polygonArr.push(projectLayer);
	},
	GeoJsonPolylineLayerName(opt) {
		let geoPolylineLayer = new SSmap.GeoJsonPolylineLayer();
		if (opt.addString) {
			geoPolylineLayer.addString(opt.addString);
		} else {
			geoPolylineLayer.geoJson = opt.url;
		}
		if (opt.color) {
			const color = color16ToRgb(opt.color);
			geoPolylineLayer.color = SSmap.Color.fromRgb(color.r, color.g, color.b, 255);
		}
		if (opt.colorField) {
			geoPolylineLayer.colorField = opt.colorField;
		}
		if (opt.enabled) {
			geoPolylineLayer.enabled = false;
		}
		if (opt.srs) {
			geoPolylineLayer.srs = opt.srs;
		}
		geoPolylineLayer.setAltitudeMethod(SSmap.AltitudeMethod[opt.altitudetype ?? 'Absolute']);
		if (opt.altitude) {
			geoPolylineLayer.setAltitude(opt.altitude);
		}
		geoPolylineLayer.usedProperties = true;
		geoPolylineLayer.setDistanceDisplayCondition(SSmap.Vector2.create(1, 900000));
		geoPolylineLayer.alpha = opt.alpha === undefined ? 1 : opt.alpha;
		geoPolylineLayer.depthTest = true;
		geoPolylineLayer.animationRun = opt.animationRun === undefined ? false : opt.animationRun; // 如果 opt.animationRun 为 undefined，则使用 true，否则使用 opt.animationRun 的值  默认false
		geoPolylineLayer.glow = opt.glow === undefined ? false : opt.glow; // 如果 opt.glow 为 undefined，则使用 true，否则使用 opt.glow 的值  默认false
		geoPolylineLayer.width = opt.width || 8.0;
		if (opt.fillColorField) {
			geoPolylineLayer.fillColorField = opt.fillColorField;
		}
		geoPolylineLayer.create();
		geoPolylineLayer.readyPromise.then(function () {
			let camera = window.viewer.scene.mainCamera.cameraController();
			camera.flyToRectangle(geoPolylineLayer.extent(), 2, 0, -90, 0);
			let rectangleExtent = geoPolylineLayer.extent().toDegrees();
		});
		PolygonNameList.push({
			geoPolylineLayer,
			id: opt.id,
		});
	},
	GeoJsonPointLayer({ geojson, icon, id, addString, key }) {
		var geoLabelLayer = new SSmap.GeoJsonLabelLayer();
		if (addString) {
			geoLabelLayer.addString(addString);
		} else {
			geoLabelLayer.geoJson = geojson;
		}
		geoLabelLayer.setAltitudeMethod(SSmap.AltitudeMethod.Absolute);
		var fields = new SSmap.StringVector(); //字符串向量集合
		var categorys = new SSmap.StringVector();
		geoLabelLayer.setNameField(key); //设置名称文本的字段 使用geojson的字段
		geoLabelLayer.setAltitude(1);
		geoLabelLayer.fontColor = SSmap.Color.fromRgb(255, 255, 255, 0); //字体颜色
		geoLabelLayer.glyphCenter = 0.7; //字形中心值
		geoLabelLayer.outlineCenter = 0.75; //描边中心值
		geoLabelLayer.strokeColor = SSmap.Color.fromRgb(0, 0, 0, 255); //描边颜色
		geoLabelLayer.url = icon;
		geoLabelLayer.imageWidth = 50;
		geoLabelLayer.imageHeight = 50;
		geoLabelLayer.scale = 2;
		geoLabelLayer.setScaleByDistance(SSmap.Vector4.create(1000, 1, 1, 1.0)); //缩放比
		geoLabelLayer.setDistanceDisplayCondition(SSmap.Vector2.create(1, 90000000000));
		geoLabelLayer.setTranslucencyByDistance(SSmap.Vector4.create(200, 1.0, 1.0e8, 0.8)); //缩放控制透明度
		geoLabelLayer.create();
		geoLabelLayer.readyPromise.then(function () {
			let camera = window.viewer.scene.mainCamera.cameraController();
			camera.flyToRectangle(geoLabelLayer.extent(), 0, 0, -90, 2);
		});
		geoPointLayerList.push({
			geoPointLayer: geoLabelLayer,
			id,
		});
	},

	//加载label标签
	addLineLayer({ addString, id }) {
		var geoLabelLayer = new SSmap.GeoJsonLabelLayer();
		geoLabelLayer.setAltitudeMethod(SSmap.AltitudeMethod.Absolute);
		var fields = new SSmap.StringVector(); //字符串向量集合
		// fields.push_back('DLMC'); //类型;
		var categorys = new SSmap.StringVector();
		// categorys.push_back('基础地名');
		geoLabelLayer.addString(addString);
		geoLabelLayer.setNameField('scenicSpotName'); //设置名称文本的字段 使用geojson的字段
		// geoLabelLayer.setTerrainField('RH'); //设置地面高度的字段 使用geojson的字段
		// geoLabelLayer.setCategory(fields, categorys); //设置分类信息标签
		//background、font、fontBold、fontItalic、fontUnderline和fontSize 无效了
		// geoLabelLayer.fontSize = 15;//已无效
		// geoLabelLayer.background = SSmap.Color.fromRgb(0, 0, 0, 255);//已无效
		// geoLabelLayer.font = "微软雅黑";//已无效
		geoLabelLayer.setAltitude(10);
		geoLabelLayer.fontColor = SSmap.Color.fromRgb(255, 255, 255, 255); //字体颜色
		geoLabelLayer.glyphCenter = 0.7; //字形中心值
		geoLabelLayer.outlineCenter = 0.75; //描边中心值
		geoLabelLayer.strokeColor = SSmap.Color.fromRgb(0, 0, 0, 255); //描边颜色
		//
		// geoLabelLayer.url = baseUrl + 'images/dot.png';
		// geoLabelLayer.imageWidth = 100;
		// geoLabelLayer.imageHeight = 20;
		// geoLabelLayer.mix = true;
		geoLabelLayer.scale = 2;
		geoLabelLayer.objectName = 'tip';
		// geoLabelLayer.setScaleByDistance(SSmap.Vector4.create(1000, 1, 1, 1.0)); //缩放比
		geoLabelLayer.setDistanceDisplayCondition(SSmap.Vector2.create(1, 90000000000));
		// geoLabelLayer.setTranslucencyByDistance(SSmap.Vector4.create(200, 1.0, 1.0e8, 0.8)); //缩放控制透明度
		geoLabelLayer.create();
		geoPointLayerList.push({
			geoPointLayer: geoLabelLayer,
			id,
		});
	},
	//加载label标签
	addLineLayertest({ addString, id, url }) {
		var geoLabelLayer = new SSmap.GeoJsonLabelLayer();
		geoLabelLayer.setAltitudeMethod(SSmap.AltitudeMethod.Absolute);
		var fields = new SSmap.StringVector(); //字符串向量集合
		// fields.push_back('DLMC'); //类型;
		var categorys = new SSmap.StringVector();
		// categorys.push_back('基础地名');
		geoLabelLayer.addString(addString);
		geoLabelLayer.setNameField('realTimeFlow'); //设置名称文本的字段 使用geojson的字段
		// geoLabelLayer.setTerrainField('RH'); //设置地面高度的字段 使用geojson的字段
		// geoLabelLayer.setCategory(fields, categorys); //设置分类信息标签
		//background、font、fontBold、fontItalic、fontUnderline和fontSize 无效了
		// geoLabelLayer.fontSize = 15;//已无效
		// geoLabelLayer.background = SSmap.Color.fromRgb(0, 0, 0, 255);//已无效
		// geoLabelLayer.font = "微软雅黑";//已无效
		geoLabelLayer.setAltitude(10);
		geoLabelLayer.fontColor = SSmap.Color.fromRgb(255, 255, 255, 0); //字体颜色
		geoLabelLayer.glyphCenter = 0.7; //字形中心值
		geoLabelLayer.outlineCenter = 0.75; //描边中心值
		geoLabelLayer.strokeColor = SSmap.Color.fromRgb(0, 0, 0, 255); //描边颜色
		geoLabelLayer.url = url;
		// geoLabelLayer.url = baseUrl + 'images/dot.png';
		geoLabelLayer.imageWidth = 30;
		geoLabelLayer.imageHeight = 45;
		// geoLabelLayer.mix = true;
		geoLabelLayer.scale = 1;
		geoLabelLayer.objectName = 'gj';
		geoLabelLayer.setScaleByDistance(SSmap.Vector4.create(1000, 1, 1, 1.0)); //缩放比
		geoLabelLayer.setDistanceDisplayCondition(SSmap.Vector2.create(1, 90000000000));
		geoLabelLayer.setTranslucencyByDistance(SSmap.Vector4.create(200, 1.0, 1.0e8, 0.8)); //缩放控制透明度
		geoLabelLayer.create();
		geoPointLayerList.push({
			geoPointLayer: geoLabelLayer,
			id,
		});
	},
	GeoJsonLineLayer(opt) {
		console.log(opt);
		let geoPolylineLayer = new SSmap.GeoJsonPolylineLayer();
		geoPolylineLayer.geoJson = opt.url;
		geoPolylineLayer.color = SSmap.Color.fromRgb(0, 0, 255, 128);
		geoPolylineLayer.alpha = 0.5;
		geoPolylineLayer.width = 1.0;
		geoPolylineLayer.create();
		PolygonNameList.push({
			geoPolylineLayer,
			id: opt.id,
		});
	},
	addModelHeat(opt) {
		// let offset = SSmap.Vector3.create(0, 0, 0);
		// let scale = SSmap.Vector3.create(1.0, 1.0, 1.0);
		// let aXis = SSmap.Vector3.create(1, 0, 0);
		// let rotation = SSmap.Quaternion.fromAxisAndAngle(aXis, -90); //x轴旋转-90度
		let model = new SSmap.Model(opt.geojson);
		// model.offset = offset;
		// model.scale = scale;
		// model.rotation = rotation;
		//model.srs = srs;  //如果有坐标系，可直接设置坐标系，如无，则在readyPromise中设置位置
		//通过entity以component的方式添加到rootEntity的子节点中，场景才会进行渲染
		var entity = new SSmap.Entity();
		entity.addComponent(model);
		console.log(model, '阿萨德噶是的');
		GlobalViewer.scene.addEntity(entity);
		model.readyPromise.then(function () {
			//如果无坐标系，可直接设置坐标信息
			let pos = SSmap.Cartographic.fromDegrees(opt.longitude, opt.latitude, opt.height);
			model.transform.cartographic = pos;
			GlobalViewer.scene.mainCamera.cameraController().flyToCartographic(
				SSmap.Cartographic.fromDegrees(opt.longitude, opt.latitude, opt.height + 100),
				2, //飞行时间，单位s
				0, //heading
				-45, //pitch
				0 //roll
			);
		});
		modelList.push({
			model: entity,
			id: opt.id,
		});
	},
	removeLine(id) {
		if (PolygonNameList.length > 0) {
			PolygonNameList.forEach((item) => {
				if (item.id == id) {
					item.geoPolylineLayer.delete();
				}
			});
			PolygonNameList = PolygonNameList.filter((item) => {
				return item.id != id;
			});
		}
	},
	removePoint(id) {
		if (geoPointLayerList.length > 0) {
			geoPointLayerList.forEach((item) => {
				if (item.id == id) {
					item.geoPointLayer.delete();
				}
			});
			geoPointLayerList = geoPointLayerList.filter((item) => {
				return item.id != id;
			});
		}
	},
	removeModel(id) {
		if (modelList.length > 0) {
			modelList.forEach((item) => {
				if (item.id == id) {
					item.model.delete();
				}
			});
			modelList = modelList.filter((item) => {
				return item.id != id;
			});
		}
	},

	Label3Dtest(obj) {
		let label3d = new SSmap.Label3D();
		label3d.position = SSmap.Cartographic.fromDegrees(obj.lon, obj.lat, 1).toVector3();
		label3d.url = obj.imageUrl;
		label3d.text = 'Test';
		label3d.background = SSmap.Color.fromRgb(0, 0, 200, 0);
		label3d.fontColor = SSmap.Color.fromRgb(255, 0, 0, 0);
		label3d.imageWidth = 75;
		label3d.imageHeight = 120;
		label3d.fontSize = 14;
		label3d.scale = 1;
		label3d.setAltitude(1);
		label3d.setAltitudeMethod(SSmap.AltitudeMethod.Absolute);
		label3d.setDistanceDisplayCondition(SSmap.Vector2.create(1, 90000000000));
		// label3d.scaleByDistance = SSmap.Vector4.create(1000, 0.4, 800000, 0.8);
		label3d.lineToGround = false;
		label3d.setCollection(SSmap.BillboardCollection.Instance());
		labelLayerList.push({
			label3d,
			id: obj.id,
		});
	},
	removeLabel3D() {
		if (labelLayerList.length > 0) {
			labelLayerList.forEach((item) => {
				item.label3d.delete();
			});
			labelLayerList = [];
		}
	},
};
